package com.internetCafes.spms.web.sys.service.impl;

import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.core.generic.GenericDao;
import com.internetCafes.spms.core.generic.GenericServiceImpl;
import com.internetCafes.spms.core.model.pageModel.DataGrid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.internetCafes.spms.web.sys.mapper.ConfInfoMapper;
import com.internetCafes.spms.web.sys.model.ConfInfo;
import com.internetCafes.spms.web.sys.service.ConfInfoService;


/**
 * 系统配置表 ConfInfoService 实现类
 *
 * @author Sunny
 * @email rekeeper2011@hotmail.com
 * @date 2018-11-12 22:26:42
 */
@Service("confInfoService")
public class ConfInfoServiceImpl extends GenericServiceImpl<ConfInfo, Long> implements ConfInfoService {

    @Autowired
    private ConfInfoMapper confInfoMapper;

    /**
     * 专业分类
     */
    @Override
    public Map<String, Map<String, List<ConfInfo>>> groupCertProfession() {
        // 大类
        Map<String, Object> params = new HashMap<>();
        params.put("type", CommonConstants.CONF_Certificate_Type);
        params.put("status", CommonConstants.CommonStatus.ENABLE);
        List<ConfInfo> pIds = query(params);
        List<String> codes = pIds.stream().map(ConfInfo::getCode).collect(Collectors.toList());
        params = new HashMap<>();
        params.put("types", codes);
        params.put("status", CommonConstants.CommonStatus.ENABLE);
        List<ConfInfo> types = query(params);
        List<String> names = types.stream().map(ConfInfo::getName).collect(Collectors.toList());
        params = new HashMap<>();
        params.put("types", names);
        params.put("status", CommonConstants.CommonStatus.ENABLE);
        List<ConfInfo> profession = query(params);
        Map<String, List<ConfInfo>> professionGroupByType = profession.stream().collect(Collectors.groupingBy(ConfInfo::getType));
        Map<String, List<ConfInfo>> typesGroupByIdType = types.stream().collect(Collectors.groupingBy(ConfInfo::getType));
        Map<String, Map<String, List<ConfInfo>>> idTypeMap = new HashMap<>();

        for (Map.Entry<String, List<ConfInfo>> typesGroupByIdTypeEntry : typesGroupByIdType.entrySet()) {
            String idTypeCode = typesGroupByIdTypeEntry.getKey();
            List<ConfInfo> idTypeTypes = typesGroupByIdTypeEntry.getValue();
            Map<String, List<ConfInfo>> typeMap = new HashMap<>();
            idTypeTypes.forEach(idTypeType -> {
                String typeName = idTypeType.getName();
                List<ConfInfo> professions
                        = professionGroupByType.get(typeName);
                typeMap.put(typeName, professions);
            });
            idTypeMap.put(idTypeCode, typeMap);

        }
        return idTypeMap;
    }

    /**
     * 获取证书类型分组
     */
    @Override
    public Map<String, List<ConfInfo>> certTypeGroup() {
        Map<String, Object> params = new HashMap<>();
        params.put("type", CommonConstants.CONF_Certificate_Type);
        params.put("status", CommonConstants.CommonStatus.ENABLE);
        List<ConfInfo> query = query(params);
        return query.stream().collect(Collectors.groupingBy(ConfInfo::getCode));
    }

    @Override
    public List<ConfInfo> query(Map<String, Object> params) {
        return confInfoMapper.selectByParams(params);
    }

    @Override
    public ConfInfo findById(Long id) {
        return confInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ConfInfo> queryByIds(List<Long> ids) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ids", ids);
        return confInfoMapper.selectByParams(params);
    }

    @Override
    public List<ConfInfo> page(Map<String, Object> params, int page, int rows) {
        params.put("page", page);
        params.put("rows", rows);
        return confInfoMapper.selectByParams(params);
    }

    @Override
    public List<ConfInfo> pageByIdDesc(Map<String, Object> params, int page, int rows) {
        params.put("page", page);
        params.put("rows", rows);
        return confInfoMapper.selectByParamsOrderByIdDesc(params);
    }

    @Override
    public long count(Map<String, Object> params) {
        return confInfoMapper.countByParams(params);
    }

    @Override
    public ConfInfo findFirst(Map<String, Object> params) {
        List<ConfInfo> list = confInfoMapper.selectByParams(params);
        ConfInfo confInfo =null;
        if (list != null && list.size() > 0) {
            confInfo =list.get(0);
        }
        return confInfo;
    }

    @Override
    public int save(ConfInfo confInfo) {
        return confInfoMapper.insert(confInfo);
    }

    @Override
    public int saveBatch(List<ConfInfo> confInfoList) {
        return confInfoMapper.insertBatch(confInfoList);
    }

    @Override
    public int saveSelective(ConfInfo confInfo) {
        return confInfoMapper.insertSelective(confInfo);
    }

    @Override
    public GenericDao<ConfInfo, Long> getDao() {
        return confInfoMapper;
    }

    @Override
    public int update(ConfInfo confInfo) {
        return confInfoMapper.updateByPrimaryKey(confInfo);
    }

    @Override
    public int updateSelective(ConfInfo confInfo) {
        return confInfoMapper.updateByPrimaryKeySelective(confInfo);
    }

    @Override
    public int delete(Long id) {
        return confInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        return confInfoMapper.deleteBatchByPrimaryKey(ids);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        if(ids != null && ids.length > 0){
            for(Long id : ids){
                delete(id);
            }
        }
    }

    /**
	 * DataGrid
	 */
    @Override
    public DataGrid dataGrid(Map<String, Object> params, int pageNo, int pageSize) {

        List<ConfInfo> ConfInfoList = page(params, (pageNo - 1 < 0 ? -1 : pageNo - 1) * pageSize, pageSize);
        long count = count(params);
        DataGrid dg = new DataGrid(ConfInfoList,count,pageSize,pageNo);

        return dg;
    }

    @Override
    public List<ConfInfo> selectAllTypes() {
        return confInfoMapper.selectAllTypes();
    }

    @Override
    public List<ConfInfo> selectByParamsUnionParent(Map<String, Object> params) {
        return confInfoMapper.selectByParamsUnionParent(params);
    }
}
